using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_administration_AddForum : System.Web.UI.UserControl
{
    public event EventHandler ForumCreated;

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            DAL.Forum myForum = new DAL.Forum();
            myForum.Name = txtName.Text;
            myForum.Description = txtDescription.Text;
            myForum.Parent = GetSelectedParentForum();
            myForum.Category = GetSelectedForumCategory();

            DAL.Forums.Add(myForum);

            lblSuccess.Visible = true;
            lblSuccess.Text = GetSuccessMessage(myForum);
            
            Refresh();
            ClearForm();

            if (ForumCreated != null)
            {
                ForumCreated(this, new EventArgs());
            }
        }
    }

    private string GetSuccessMessage(DAL.Forum newForum)
    {
        return string.Format("<p><span style=\"color : #ff0000;\"><b>{0}</b> was successfully created.</span></p>",
            Server.HtmlEncode(newForum.Name));
    }

    public void Refresh()
    {
        DAL.ForumCategory category = GetSelectedForumCategory();
        if (category != null)
        {
            mviewForum.SetActiveView(viewForum);

            lstForum.Items.Clear();
            lstForum.Items.Add("(none)");
            AddForums(category.GetForums(), 0);
        }
        else
        {
            mviewForum.SetActiveView(viewNone);
        }
    }

    private void ClearForm()
    {
        txtName.Text = "";
        txtDescription.Text = "";
        ddlVisibility.SelectedIndex = 0;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            mviewForum.SetActiveView(viewNone);

            lstCategory.DataSource = DAL.ForumCategories.All();
            lstCategory.DataBind();
            lstCategory.Items.Insert(0, "(none)");
            Refresh();
        }
    }

    private DAL.ForumCategory GetSelectedForumCategory()
    {
        long id;
        if (long.TryParse(lstCategory.SelectedValue, out id))
        {
            return DAL.ForumCategories.Item(id);
        }
        else
        {
            return null;
        }
    }

    private DAL.Forum GetSelectedParentForum()
    {
        long id;
        if (long.TryParse(lstForum.SelectedValue, out id))
        {
            return DAL.Forums.Item(id);
        }
        else
        {
            return null;
        }
    }

    protected void lstCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        Refresh();
    }

    private void AddForums(List<DAL.Forum> forums, int depth)
    {
        foreach(DAL.Forum forum in forums)
        {
            ListItem item = new ListItem();
            item.Text = new string(' ', depth * 2) + forum.Name;
            item.Value = forum.ID.ToString();
            lstForum.Items.Add(item);

            AddForums(forum.GetChildForums(), depth + 1);
        }
    }
}
